﻿using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;


namespace CRMS.Models
{
    public class TeacherRep
    {
        private StoreDBDataContext storeDB = new StoreDBDataContext();
        //
        //Query Methods
        /// <summary>
        /// 获取所有的教师
        /// </summary>
        /// <returns>返回教师集合</returns>
        public IQueryable<Teacher> GetTeachers()
        {
            return from t in storeDB.Teachers select t;
        }
        /// <summary>
        /// 根据院系ID获取其所有的老师
        /// </summary>
        /// <param name="did">院系ID</param>
        /// <returns>返回所有所属院系老师的集合</returns>
        public IQueryable<Teacher> GetTeacherByDID(string did)
        {
            return storeDB.Teachers.Where(t => t.DepartmentId == did);
        }
        /// <summary>
        /// 根据院系名字获取所有的老师
        /// </summary>
        /// <param name="dName">院系Name</param>
        /// <returns>返回所有所属院系老师的集合</returns>
        public IQueryable<Teacher> GetTeacherByDName(string dName)
        {
            return storeDB.Teachers.Where(t => t.Department.DepartmentName == dName);
        }
        /// <summary>
        /// 根据老师的名字从数据库里读出一个老师的详细记录
        /// </summary>
        /// <param name="name">老师Name</param>
        /// <returns>返回老师对象</returns>
        public Teacher GetTeacherByName(string name)
        {
            return storeDB.Teachers.SingleOrDefault(t => t.TeacherName == name);
        }
        /// <summary>
        /// 根据老师的ID从数据库里读出一个老师的详细记录
        /// </summary>
        /// <param name="id">老师ID</param>
        /// <returns>返回老师对象</returns>
        public Teacher GetTeacherByID(string id)
        {
            return storeDB.Teachers.SingleOrDefault(t => t.TeacherId == id);
        }
        /// <summary>
        /// 根据课程的编号，获取所有开设该课程的老师信息
        /// </summary>
        /// <param name="cid">课程编号</param>
        /// <returns>开设该课程的所有老师</returns>
        public IQueryable<Teacher> GetTeachersByCourseID(string cid)
        {
            var tid = storeDB.TeacherCourses.Where(tc => tc.CourseId == cid).Select(t => t.TeacherId);
            return storeDB.Teachers.Where(t =>tid.Contains(t.TeacherId));
        }

        public bool HasTeacher(string tName,string tID)
        {
            return storeDB.Teachers.Any(t => t.TeacherName == tName && t.TeacherId == tID);
        }

        //
        //Insert/Delete
        public void Add(Teacher obj)
        {
            storeDB.Teachers.InsertOnSubmit(obj);
        }
        public void Delete(Teacher obj)
        {
            storeDB.TeacherCourses.DeleteAllOnSubmit(obj.TeacherCourses);
            storeDB.Teachers.DeleteOnSubmit(obj);
        }
        //
        //Persitence
        public void Save()
        {
            storeDB.SubmitChanges();
        }
        //通过课程名和学院ID来获取教师信息
        public IQueryable<Teacher> getTeachersInfo(string departmentId, string courseName)
        {

            return storeDB.Teachers.Where(t => t.Department.DepartmentId == departmentId && 
                (from tc in storeDB.TeacherCourses
                 where tc.Course.CourseName == courseName
                 select tc.TeacherId).Contains(t.TeacherId));
        }

    }
}
